/*
Copyright 2015 shizhefei（LuckyJayce）

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */
package com.pkutech.carcasher.content;

import com.pkutech.carcasher.db.RealmManager;
import com.pkutech.carcasher.model.Message;
import com.pkutech.carcasher.model.MsgDB;
import com.pkutech.carcasher.utils.CommonUtils;
import com.rengwuxian.materialedittext.validation.METLengthChecker;
import com.shizhefei.mvc.IDataCacheLoader;
import com.shizhefei.mvc.IDataSource;
import java.util.ArrayList;
import java.util.List;

public class MessageDataSource implements IDataSource<List<Message>>, IDataCacheLoader<List<Message>> {
	private int page = 1;
	private int maxPage = 10;

	List<Message> messages = new ArrayList<Message>();

	public List<Message> getMessages() {
		return messages;
	}

	public void setMessages(List<Message> messages) {
		this.messages = messages;
	}

	/**
	 * 加载缓存<br>
	 * 注意这个方法执行于UI线程，不要做太过耗时的操作<br>
	 * 每次刷新的时候触发该方法，该方法在DataSource refresh之前执行<br>
	 *
	 * @param isEmpty
	 *            adapter是否有数据，这个值是adapter.isEmpty()决定
	 * @return 加载的数据，返回后会执行adapter.notifyDataChanged(data, true)<br>
	 *         相当于refresh执行后adapter.notifyDataChanged(data, true)
	 */
	@Override
	public List<Message> loadCache(boolean isEmpty) {
		if (isEmpty) {
//			List<Message> selectBookingOrders = RealmManager.findAllSelectBookingOrders();
			return messages;
		}
		return null;
	}

	@Override
	public List<Message> refresh() throws Exception {
		return loadData(1);
	}

	@Override
	public List<Message> loadMore() throws Exception {
		return loadData(page + 1);
	}

	private List<Message> loadData(int page) throws Exception {
		// 如果网路出错的话，直接抛异常不会执行后面的获取的语句
		//TODO
//		SoapApi.appSelectBookingOrder(RealmManager.findUserInfo().getCode())
//				.subscribe(new Subscriber<SelectBookingOrderResponse>() {
//					@Override
//					public void onCompleted() {
//
//					}
//
//					@Override
//					public void onError(Throwable e) {
//
//					}
//
//					@Override
//					public void onNext(SelectBookingOrderResponse selectBookingOrderResponse) {
//						if(selectBookingOrderResponse != null && !TextUtils.isEmpty(selectBookingOrderResponse.getStatus()) && "true".equals(selectBookingOrderResponse.getStatus())){
//							netSelectBookingOrders = selectBookingOrderResponse.getData();
//							if(netSelectBookingOrders != null && netSelectBookingOrders.size() > 0){
//								maxPage = CommonUtils.calAllPageCount(netSelectBookingOrders.size());
//								RealmManager.clearSelectBookingOrders();
//								RealmManager.saveSelectBookingOrders(netSelectBookingOrders);
//							}
//						}
//					}
//				});
//		Log.d("cyy", "maxPage:"+maxPage);
		this.page = page;
		messages.clear();
		List<MsgDB> msgDBList = RealmManager.findAllMessage();
		if(msgDBList != null && msgDBList.size() > 0){
			maxPage = CommonUtils.calAllPageCount(msgDBList.size());
			for(MsgDB msgDB : msgDBList){
				Message msg = new Message();
				msg.setMsg(msgDB.getMsg());
				msg.setTime(msgDB.getTime());
				messages.add(msg);
			}
		}
		return messages;
	}

	@Override
	public boolean hasMore() {
		return page < maxPage;
	}

}
